pascal triangle in php (anything wrong with this solution?) [migrated]
Posted
by
zhenka
on Programmers
See other posts from Programmers
or by zhenka
Published on 2012-09-21T17:11:12Z
Indexed on
2012/09/21
21:55 UTC
Read the original article
Hit count: 157
I saw that one of the interview questions could be building a pascal triangle. Is there anything wrong with this particular solution I came up with?
function pascal_r($r){
$local = array();
if($r == 1){
return array(array(1));
} else {
$previous = pascal_r($r - 1);
array_push($local, 1);
for($i = 0; $i < $r - 2 && $r > 2; $i++){
array_push($local, $previous[$r-2][$i] + $previous[$r-2][$i + 1]);
}
array_push($local, 1);
}
array_push($previous, $local);
return $previous;
}
print_r(pascal_r(100));
© Programmers or respective owner